// member/pages/coupon/coupon.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    status: 1,
    qrType: false,
    imgQRCode:''
  },
  toOfflineOrderDetails(e) {
    my.navigateTo({
      url: `/member/pages/offlineOrderDetails/offlineOrderDetails?order_sn=${e.target.dataset.order_sn}`,
    })
  },
  //显示二维码
  showQr(e) {
    const that = this;
    that.setData({
      qrcode: e.currentTarget.dataset.qrcode,
      qrType: true
    })
    my.getSystemInfo({
      success: (prop) => {
        const viewWidth = parseInt(prop.windowWidth * 0.9);
        my.generateImageFromCode({
          code:e.currentTarget.dataset.qrcode, // 必填
          format: 'QRCODE', // 或'BARCODE'
          width: viewWidth, // 图片宽度(px)
          success: (res) => {
            this.setData({
              imgQRCode: res.image
            });
          },
          fail: (err) => {
            console.log(err)
          }
        });

        
      }
    })
  },
  remQr() {
    const that = this;
    that.setData({
      qrType: false
    })
  },
 
  navClick(e) {
    let that = this;
    if (e.target.dataset.status == that.data.status) {
      return;
    }
    that.setData({
      status: e.target.dataset.status,
      page: 1,
      isEnd: true,
      isWithout: true,
      listData: []
    })
    that.getData()
  },
  getData() {
    const that = this;
    let data = {
      page: that.data.page,
      order_type: parseInt(that.data.status)
    }
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.offlineOrderList, data).then(res => {
      my.hideLoading();
      my.stopPullDownRefresh();
      my.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {

          that.setData({
            isWithout: true,
            listData: res.data.result.list
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    my.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })

    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      page: that.data.page + 1,
      order_type: parseInt(that.data.status)
    }
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.offlineOrderList, data).then(res => {
      my.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          my.showToast({
            content: '没有更多了',
            type: 'none',
          })
          that.setData({
            isEnd: false
          })
        } else {

          that.setData({
            listData: [...that.data.listData, ...res.data.result.list],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})